package com.fingard.dsp.bank.directbank.jxb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.io.FileHelper;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

import java.io.File;
import java.util.Date;

/**
 * Created by F on 2019/08/07
 */
public class JXB01Base extends DirectBase{

    /**
     * 报文头
     * @param xmlWriter
     * @param p_bankHeader
     */
    protected void writeReqHeader(XmlTextWriter xmlWriter, JXB01Header p_bankHeader){
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\" ?>");
        xmlWriter.writeStartElement("Message");
        xmlWriter.writeStartElement("Head");
        xmlWriter.writeElementString("TrsCode", p_bankHeader.trsCode);//交易代码
        xmlWriter.writeElementString("ERPTrsTimestamp", Format.DateTimeFormat.format(new Date()));//客户端请求时间yyyy-mm-dd hh:mm:ss
    }

    /**
     * 报文编码
     * @return
     */
    protected String getCharset(){
        return getBankConfig().getCharset("UTF-8");
    }

    /**
     * 请求银行
     * @param p_reqStr
     * @return
     * @throws Exception
     */
    protected String[] sendToBank(String p_reqStr,String transCode) throws Exception {
        String[] retStr = new String[]{"", ""};
        String serverURL = "http://" + bankFront.serverIP + ":" + bankFront.serverPort +"/eweb/"+transCode+".do" + "?userPassword="+bankFront.loginPasswd
                +"&SIGDATA=1";
        WriteBankLogLn("请求银行地址："+serverURL);
        WriteBankLogStep2(p_reqStr);
        WebRequest tmpWebRequest = new WebRequest(serverURL);
        byte[] tmpToWriteBytes = p_reqStr.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpToWriteBytes, getCharset());
        WriteBankLogStep3(retStr[1]);
        return retStr;
    }
}
